package org.thingsboard.server.dao.sql.schedulerEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.SchedulerEventInfo;
import org.thingsboard.server.common.data.SchedulerEventStatus;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.model.sql.SchedulerEventInfoEntity;
import org.thingsboard.server.dao.schedulerEvent.SchedulerEventInfoDao;
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Component
public class JpaSchedulerEventInfoDao extends JpaAbstractSearchTextDao<SchedulerEventInfoEntity, SchedulerEventInfo> implements SchedulerEventInfoDao {
    @Autowired
    SchedulerEventInfoRepository schedulerEventInfoRepository;

    @Override
    public Long countByTenantId(TenantId tenantId) {
        return schedulerEventInfoRepository.countByTenantId(tenantId.getId());
    }
    @Override
    protected Class<SchedulerEventInfoEntity> getEntityClass() {
        return SchedulerEventInfoEntity.class;
    }


    @Override
    protected CrudRepository<SchedulerEventInfoEntity, UUID> getCrudRepository() {
        return schedulerEventInfoRepository;
    }


    @Override
    public PageData<SchedulerEventInfo> findSchedulerEventInfosByTenantId(UUID tenantId, PageLink pageLink) {
        return DaoUtil.toPageData(schedulerEventInfoRepository
                .findByTenantId(
                        tenantId,
                        Objects.toString(pageLink.getTextSearch(), ""),
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<SchedulerEventInfo> findSchedulerEventInfosByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink) {
        return DaoUtil.toPageData(schedulerEventInfoRepository
                .findByTenantIdAndCustomerId(
                        tenantId,
                        customerId,
                        Objects.toString(pageLink.getTextSearch(), ""),
                        DaoUtil.toPageable(pageLink)));
    }


    @Override
    public List<SchedulerEventInfo> findSchedulerEventInfosByStatus(SchedulerEventStatus status, PageLink pageLink) {
        return DaoUtil.toPageData(schedulerEventInfoRepository
                .findSchedulerEventInfosByStatus(
                        status,
                        DaoUtil.toPageable(pageLink))).getData();
    }

}
